// pages/event_detail/event_detail.js
import { request } from "../../request/index.js"
import { showToast, downloadFile, openDocument } from "../../utils/asyncWx.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    event: {},
    eventUser: {},
    eventUsersInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 保存页面跳转参数
    this.setData({ eventid: options.eventid })

    let data = { eventid: options.eventid }
    // 根据eventid 查询事件详情
    const res = await request({ url: "/AttendanceEvent/getEvent", data })
    let event = res.data.data
    data = { eventid: options.eventid }

    // 获取EventUser 通过eventid和openid  以获得打卡状态
    const result = await request({ url: "/EventUser/getEventUser", data })
    let eventUser = result.data.data
    this.setData({ event, eventUser }) // 保存event,eventUser

    console.log("eventUser:>>>>>>", eventUser)
    // 如果是考勤者，请求事件相关的所有打卡者信息
    if (eventUser.identity === 1) {
      let data = { eventid: options.eventid }
      const response = await request({ url: "/EventUser/getEventUsersByEventid", data })
      if (response.data != null)
        this.setData({ eventUsersInfo: response.data.data })
      // 

    }
  },
  // 导出报表
  async exportReport() {
    // 下载文件
    const res = await downloadFile({
      url: "/ExportReport/getReportByExcel",
      header: {
        eventid: this.data.event.eventid
      }
    })
    // 打开文件并开启菜单
    const res1 = await openDocument({
      filePath: res.tempFilePath,
      showMenu: true, // 关键点
    })
    console.log(res1);
  },
  // 删除事件
  async deleteEvent() {
    const { eventid, beaconId } = this.data.event
    let data = {
      eventid,
      beaconId
    }
    // 封装请求头
    let header = { "Content-Type": "application/x-www-form-urlencoded" }
    const res = await request({ url: "/AttendanceEvent/deleteEvent", data, method: "POST", header })
    console.log(res)
    if (res.data.data != 0) {
      showToast({ title: "删除成功！" })
      // 返回上一页
      wx.navigateBack({
        delta: 1
      })
    } else {
      showToast({ title: "删除失败" })
    }
  },
  // 解绑Beacon
  async unBind() {
    const { beaconId } = this.data.event
    const res = await request({ url: "/Beacon/unBind", data: { beaconId } })
    console.log(res)
    if (res.data.data != 0) {
      // 解绑成功 要改变事件显示状态
      showToast({ title: "解绑成功！" })
      // 刷新页面，更新数据
      const eventid = this.data.event.eventid
      this.onLoad({ eventid })
    } else {
      showToast({ title: "解绑失败" })
    }
  },

  /**
  * 用户点击button分享
  */
  onShareAppMessage(res) {
    // console.log("onShareAppMessage",res);
    // let { eventid } = res.target.dataset.info;
    let { eventid } = this.data.event

    return {
      title: '分享打卡事件',
      path: '/pages/find_event/find_event?eventid=' + eventid,
      // path: '/pages/find_event/find_event',
      imageUrl: 'https://pic.imgdb.cn/item/60742ebb8322e6675cb3b97f.jpg',
      success: function (res) {
        console.log("success:", res);
        let shareTicket = res.shareTickets[0];
        console.log(shareTicket);
        // 转发成功
        wx.showToast({
          title: "分享成功",
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        console.log("fail:", res)
        // 分享失败
      }
    }
  }

})